﻿namespace EFS.RC.Survey.Controls
{
	/// Class List
	public partial class List : EFS.RC.Survey.Controls.Control
	{
		/*
		 * - add column with user 
		 * - remove view from first table
		 * - add second table with open
		 * - in first table show all available surveys not already startef by me
		 */

		#region Locals
		/// modalHostParent
		private EFS.Common.UI.Controls.ModalDialogHost modalHostParent = null;

		/// reviewType
		private EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewTypeSessions = null;
		#endregion

		#region Constructors
		/// Implicit
		public List()
		{
			InitializeComponent();

			ConstructorNotAllowed();
		}

		/// Explicit
		public List(
			EFS.Common.UI.Controls.ModalDialogHost modalHostParent,
			System.Guid? conversationGuid,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewTypeSessions)
		{
			InitializeComponent();

			Init(modalHostParent, conversationGuid, null, reviewTypeSessions);
		}

		/// Explicit
		public List(
			EFS.Common.UI.Controls.ModalDialogHost modalHostParent,
			System.String documentId,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewTypeSessions)
		{
			InitializeComponent();

			Init(modalHostParent, null, documentId, reviewTypeSessions);
		}

		/// Init
		private void Init(
			EFS.Common.UI.Controls.ModalDialogHost modalHostParent,
			System.Guid? conversationGuid,
			System.String documentId,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewTypeSessions)
		{
			// ignore in designer
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listener up for list load
				UtilitiesService.Survey_GetAllAvailableConversationSurveysForUserCompleted += new System.EventHandler<Utilities.Survey_GetAllAvailableConversationSurveysForUserCompletedEventArgs>(UtilitiesService_Survey_GetAllAvailableConversationSurveysForUserCompleted);
				UtilitiesService.Survey_GetAllConversationSurveySessionsCompleted += new System.EventHandler<Utilities.Survey_GetAllConversationSurveySessionsCompletedEventArgs>(UtilitiesService_Survey_GetAllConversationSurveySessionsCompleted);
				UtilitiesService.Survey_GetAllAvailableDocumentsSurveysForUserCompleted += new System.EventHandler<Utilities.Survey_GetAllAvailableDocumentsSurveysForUserCompletedEventArgs>(UtilitiesService_Survey_GetAllAvailableDocumentSurveysForUserCompleted);
				UtilitiesService.Survey_GetAllDocumentSurveySessionsCompleted += new System.EventHandler<Utilities.Survey_GetAllDocumentSurveySessionsCompletedEventArgs>(UtilitiesService_Survey_GetAllDocumentSurveySessionsCompleted);

				// set
				this.modalHostParent = modalHostParent;
				this.ConversationGuid = conversationGuid;
				this.DocumentId = documentId;
				this.reviewTypeSessions = reviewTypeSessions;
			}
		}
		#endregion

		#region Properties

		/// Property ConversationGuid: Get, Set
		public System.Guid? ConversationGuid { get; private set; }

		/// Property DocumentId: Get, Set
		public System.String DocumentId { get; private set; }

		#endregion

		#region Delegates

		/// <summary>
		/// Delegate to open survey
		/// </summary>
		/// <param name="surver"></param>
		/// <returns></returns>
		//public delegate void OpenSurvey(EFS.RC.CC.CH.Entities.Surveys.SurveyInfo survey);
		public delegate void OpenSurvey(EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo survey);


		/// <summary>
		/// Open survey delgate registrar
		/// </summary>
		public OpenSurvey OnOpenSurvey = null;

		#endregion

		#region LoadData
		/// LoadData
		private void LoadData()
		{
			// Load surveys
			LoadSurveys();
		}

		/// Loads
		private void LoadSurveys()
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress("saved", "Loading saved Surveys for Conversation");
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress("all", "Loading Surveys");

			//call ws
			if (null != ConversationGuid)
			{
				UtilitiesService.Survey_GetAllAvailableConversationSurveysForUserAsync(ConversationGuid.Value, (System.Int32)EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType.Agent);
				UtilitiesService.Survey_GetAllConversationSurveySessionsAsync(ConversationGuid.Value, (System.Int32?)reviewTypeSessions);
			}
			else if (null != DocumentId)
			{
				UtilitiesService.Survey_GetAllAvailableDocumentsSurveysForUserAsync(DocumentId, (System.Int32)EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType.Agent);
				UtilitiesService.Survey_GetAllDocumentSurveySessionsAsync(DocumentId, (System.Int32?)reviewTypeSessions);
			}
		}
		#endregion

		#region WS callbacks

		/// UtilitiesService_Survey_GetAllAvailableConversationSurveysForUserCompleted
		void UtilitiesService_Survey_GetAllAvailableConversationSurveysForUserCompleted(object sender, Utilities.Survey_GetAllAvailableConversationSurveysForUserCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed("all");

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveys =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[]>(e.Result.ToString());

				// bind/fill form
				dataGridSurveys.ItemsSource = surveys;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data.", ex);
			}
		}

		//UtilitiesService_Survey_GetAllConversationSurveySessionsCompleted
		void UtilitiesService_Survey_GetAllConversationSurveySessionsCompleted(object sender, Utilities.Survey_GetAllConversationSurveySessionsCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed("saved");

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] surveySessions =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[]>(e.Result.ToString());

				// bind/fill form
				dataGridSurveySessions.ItemsSource = surveySessions;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data.", ex);
			}
		}

		/// UtilitiesService_Survey_GetAllAvailableDocumentSurveysForUserCompleted
		void UtilitiesService_Survey_GetAllAvailableDocumentSurveysForUserCompleted(object sender, Utilities.Survey_GetAllAvailableDocumentsSurveysForUserCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed("all");

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveys =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[]>(e.Result.ToString());

				// bind/fill form
				dataGridSurveys.ItemsSource = surveys;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data.", ex);
			}
		}

		//UtilitiesService_Survey_GetAllDocumentSurveySessionsCompleted
		void UtilitiesService_Survey_GetAllDocumentSurveySessionsCompleted(object sender, Utilities.Survey_GetAllDocumentSurveySessionsCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed("saved");

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] surveySessions =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[]>(e.Result.ToString());

				// bind/fill form
				dataGridSurveySessions.ItemsSource = surveySessions;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data.", ex);
			}
		}
		#endregion

		#region Events

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in designer
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}

		/// dataGridSurveys_LoadingRow
		private void dataGridSurveySessions_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
		{
			// get survey data
			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo surveySessionDetails = (EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo)e.Row.DataContext;

			// test data
			if (null == surveySessionDetails)
			{
				return;
			}

			// VIEW
			System.Windows.Controls.HyperlinkButton hyperlinkButtonView = dataGridSurveySessions.Columns[4].GetCellContent(e.Row) as System.Windows.Controls.HyperlinkButton;
			if (null != hyperlinkButtonView)
			{
				if (null != ConversationGuid)
				{
					hyperlinkButtonView.NavigateUri =
						new System.Uri(
							EFS.RC.Survey.Controls.Common.Main.GetReportViewURL(
								EFS.RC.Survey.Controls.Common.Config.Reports.SurveyConversationReportName,
								new System.String[] { "conversationGUID", "surveyID" },
								new System.String[] { EFS.Common.Functions.Guid.Strip(ConversationGuid.Value), surveySessionDetails.Survey.ID.Value.ToString() }));
				}
				else
					if (null != DocumentId)
					{
						hyperlinkButtonView.NavigateUri =
							new System.Uri(
								EFS.RC.Survey.Controls.Common.Main.GetReportViewURL(
									EFS.RC.Survey.Controls.Common.Config.Reports.SurveyDocumentReportName,
									new System.String[] { "documentId", "surveyID" },
									new System.String[] { DocumentId, surveySessionDetails.Survey.ID.Value.ToString() }));
					}
			}
		}

		/// <summary>
		/// Start survey
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void StartSurvey_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// close this first
			this.modalHostParent.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);

			// get data
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo survey = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo)dataGridSurveys.SelectedItem;

			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo sessionInfo = new EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo();
			sessionInfo.Survey = new EFS.RC.CC.CH.Entities.Surveys.SurveyInfo();
			sessionInfo.Survey = survey;
			sessionInfo.ReadOnly = false;
			if (null != survey && null != OnOpenSurvey)
			{
				OnOpenSurvey(sessionInfo);
			}
		}

		/// <summary>
		/// Open
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OpenSurvey_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// close this first
			this.modalHostParent.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);

			// get data
			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo sessionInfo = (EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo)dataGridSurveySessions.SelectedItem;
			sessionInfo.ReadOnly = false;

			if (null != sessionInfo && null != OnOpenSurvey)
			{
				OnOpenSurvey(sessionInfo);
			}
		}

		#endregion
	}

}